package attendance;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class Fetcher {
    private Map<String, Integer> columnMap = new HashMap<String, Integer>();
    private static Map<String, String> columnNameMap = new HashMap<String, String>();
    static {
        columnNameMap.put("部门班组", "GROUP_KEY");
        columnNameMap.put("用户编号", "ID_KEY");
        columnNameMap.put("姓名", "NAME_KEY");
        columnNameMap.put("日期", "DATE_KEY");
        columnNameMap.put("上午上班", "ARRIVE_KEY");
        columnNameMap.put("上班描述", "ARRIVE_NOTE_KEY");
        columnNameMap.put("下午下班", "LEAVE_KEY");
        columnNameMap.put("下班描述", "LEAVE_NOTE_KEY");
        columnNameMap.put("迟到分钟", "LATE_KEY");
        columnNameMap.put("早退分钟", "EARLY_KEY");
        columnNameMap.put("加班小时", "OVERTIME_KEY");
        columnNameMap.put("旷工[天]", "ABSENT_KEY");
        columnNameMap.put("未刷次数", "MISS_KEY");
    }
    
    public List<Staff> fetch(String fileName) throws FileNotFoundException, IOException {
        Workbook wb = new HSSFWorkbook(new FileInputStream(fileName));
        Sheet sheet = wb.getSheetAt(0);
        return fetch(sheet);
    }

    private List<Staff> fetch(Sheet sheet) {
    	List<Staff> staffList = new ArrayList<Staff>();
        Iterator<Row> rowIterator = sheet.rowIterator();
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            if (row.getRowNum() == 0) {
                fetchHeader(row);
            } else {
                Attendance attendance = fetchAttedance(row);
                if (attendance == null) {
                    continue;
                }
                if (attendance != null && staffList.contains(attendance.getStaff())) {
                	for (Staff staff : staffList) {
                		if (staff.equals(attendance.getStaff())) {
                			staff.getAttendanceList().add(attendance);
                		}
                	}
                } else {
                	Staff staff = attendance.getStaff();
                	staff.getAttendanceList().add(attendance);
                	staffList.add(staff);
                }
            }
        }

        return staffList;
    }

    private Staff fetchStaff(Row row) {
        Staff staff = null;
//        staff.setId(row.getCell(columnMap.get("ID_KEY")).getStringCellValue());
        Cell cell = row.getCell(columnMap.get("NAME_KEY"));
        if (cell != null) {
            staff = new Staff();
            staff.setName(cell.getStringCellValue());
            cell = row.getCell(columnMap.get("GROUP_KEY"));
            if (cell == null) {
                staff.setGroup("空白");
            } else {
                staff.setGroup(cell.getStringCellValue());
            }
        }

        return staff;
    }
    
    private Attendance fetchAttedance(Row row) {
        Attendance attendance = null;
        Staff staff = fetchStaff(row);
        if (staff == null) {
            return attendance;
        }
        attendance = new Attendance();
        attendance.setStaff(staff);
        
        String dateString = row.getCell(columnMap.get("DATE_KEY")).getStringCellValue().split(" ")[0];
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
			attendance.setDate(format.parse(dateString));
		} catch (ParseException e) {
			e.printStackTrace();
		}
        
        Cell cell = null;
        
        cell = row.getCell(columnMap.get("ARRIVE_NOTE_KEY"));
        if (cell != null) {
        	attendance.setArriveNote(cell.getStringCellValue());
        	
        }
        cell = row.getCell(columnMap.get("LEAVE_NOTE_KEY"));
        if (cell != null) {
        	attendance.setLeaveNote(cell.getStringCellValue());
        }
        
        attendance.setLateMinute(row.getCell(columnMap.get("LATE_KEY")).getNumericCellValue());
        attendance.setEarlyMinute(row.getCell(columnMap.get("EARLY_KEY")).getNumericCellValue());
        attendance.setOvertimeHour(row.getCell(columnMap.get("OVERTIME_KEY")).getNumericCellValue());
        attendance.setAbsentDay(row.getCell(columnMap.get("ABSENT_KEY")).getNumericCellValue());
        attendance.setMissCount(row.getCell(columnMap.get("MISS_KEY")).getNumericCellValue());
        
        return attendance;
    }

    private void fetchHeader(Row row) {
        Iterator<Cell> cellIterator = row.cellIterator();
        while (cellIterator.hasNext()) {
            Cell cell = cellIterator.next();
            String content = cell.getStringCellValue();
            String columnKey = columnNameMap.get(content);
            if (columnKey == null) {
                continue;
            }
            columnMap.put(columnKey, cell.getColumnIndex());
        }
    }
    
}
